﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.ArrivalNotice
{
    public class OutboundListHandler : IRequestHandler<OutboundListCommand, ResultsDTO<OutboundDto>>
    {
        #region 仓储
        private readonly IBateRepository<OutboundDto> OutboundDtoRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="OutboundDtoRepository"></param>
        public OutboundListHandler(IBateRepository<OutboundDto> OutboundDtoRepository)
        {
            this.OutboundDtoRepository = OutboundDtoRepository;
        }
        #endregion

        #region 出库统计
        /// <summary>
        /// 出库统计
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO<OutboundDto>> Handle(OutboundListCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultDTO<OutboundDto>对象result，并设置Code为 0
                ResultsDTO<OutboundDto> result = new ResultsDTO<OutboundDto>();

                DapperHelper helper = new DapperHelper();
                OutboundDto dto = new OutboundDto();

                //连接mySql 
                //定义一个字符串变量conn  将dapperHelper对象的MangoMesWrite属性的值赋给它
                string conn = helper.MangoMesWrite;

                //sql语句
                string str = $"select b.productManageNode,b.productManageName,b.productManageNumber,b.unites,c.StashName,a.StickWorkNum,a.StockWorkTime from stockremoval a JOIN productmanage b on a.productManageNode=b.productManageNode join warehouselist c on a.WarehouseId=c.StashId";

                var list = OutboundDtoRepository.GetAll(str, dto, conn).ToList();
                result.Data = list;

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}